<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/21
 * Time: 14:28
 */
namespace app\admin\controller;
use app\data\Data;

/**
 * -----------------------------------------
 * Class Index
 * @package app\admin\controller
 * -----------------------------------------
 */
class Index extends Check{
    /**
     * @return \think\response\View
     * 后台首页
     */
    public function index(){
        $aid = empty(is_login()) ? 0 : is_login();
        $config_model = new Data('config');
        $config = [];
        $result = $config_model->selectData();
        if(!empty($result)){
            $config = \think\Helper::config($result);
        }
        $admin = new Data('admin');
        $auth_rule = new Data('auth_rule');
        $ruleIds = $admin->joinOne([['auth_group_access b','a.id = b.uid','left'],['auth_group c','b.group_id = c.id','left']],"a.id = $aid",'a.*,c.rules'); //获取当前用户具备的权限
        $ruleIds = empty($ruleIds) ? 0 : $ruleIds;
        if(empty($ruleIds)){
            $this->error('你不具备任何管理权限无法登录');
        }
        $ruleIds['rules'] = empty($ruleIds['rules']) ? 0 : $ruleIds['rules'];
        if($aid == $admin::ROLE_ADMIN){
            $parent_button = $auth_rule->selectData("p_id = 0",'','orderby asc');
            $son_button    = $auth_rule->selectData("p_id != 0 and is_show = 0",'','orderby asc');
        }else{
            $parent_button = $auth_rule->selectData("id in ({$ruleIds['rules']}) and p_id = 0",'','orderby asc');
            $son_button    = $auth_rule->selectData("id in ({$ruleIds['rules']}) and p_id != 0 and is_show = 0",'','orderby asc');
        }
        $button = \think\Helper::button($parent_button,$son_button);
        return view('',['result'=>$config,'button'=>$button]);
    }

    /**
     * @return \think\response\View
     * 后台主体页面
     */
    public function main(){
        $config_model = new Data('config');
        $config = [];
        $result = $config_model->selectData();
        if(!empty($result)){
            foreach($result as $v){
                $config[$v['cname']] = $v['option'];
            }
        }
        return view('',['result'=>$config]);
    }
}